<template>
  <div class="menu-create">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>{{ isEdit ? '编辑广告' : '添加广告' }}</span>
      </div>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="广告名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="广告位置">
          <el-select v-model="form.spaceId" placeholder="请选择上级菜单">
            <!-- <el-option :value="-1" label="无上级菜单"></el-option> -->
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in advertSpaceList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="form.startTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期时间">
          <el-date-picker
            v-model="form.endTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="前端图标">
          <course-image v-model="form.img" />
          <!-- <el-input v-model="form.img"></el-input> -->
        </el-form-item>
        <el-form-item label="上线/下线">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">下线</el-radio>
            <el-radio :label="1">上线</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="广告链接">
          <el-input v-model="form.link"></el-input>
        </el-form-item>
        <el-form-item label="广告备注">
          <el-input type="textarea" v-model="form.text"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button v-if="!isEdit">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { createOrUpdateAdvert, getEditAdvertInfo, uploadAdvertImg } from '@/services/advert'
import { getQueryAdvertSpace } from '@/services/advert-space'
import CourseImage from './CourseImage.vue'

export default Vue.extend({
  name: 'MenuCreateOrEdit',
  components: {
    CourseImage
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      advertSpaceList: []
    }
  },

  created() {
    this.getQueryAdvertSpace()
    this.loadMenuInfo()
  },

  methods: {
    async loadMenuInfo() {
      const { data } = await getEditAdvertInfo(this.$route.params.id || -1)
      if (data.success) {
        this.form = data.content
      }
    },
    async getQueryAdvertSpace() {
      const { data } = await getQueryAdvertSpace()
      this.advertSpaceList = data.content
    },
    async onSubmit() {
      // 1. 表单验证
      // 2. 验证通过，提交表单
      const { data } = await createOrUpdateAdvert(this.form)
      // console.log(data)
      if (data.success) {
        this.$message.success('提交成功')
        this.$router.back()
      }
    }
  }
})
</script>

<style lang="scss" scoped></style>
